package com.ruoyi.web.controller.inventory;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.InvInventoryCheck;
import com.ruoyi.system.domain.InvInventoryWorkOrder;
import com.ruoyi.system.service.InvInventoryCheckService;
import com.ruoyi.system.service.InvInventoryWorkOrderService;
import com.ruoyi.system.util.AbdulEmptyUtils;
import com.ruoyi.system.util.BaseResponse;
import com.ruoyi.system.util.StoreIdUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/check")
@Api(tags = "库存盘点详情管理")
public class InvInventoryCheckController {

    @Autowired
    private InvInventoryCheckService checkService;
    @Autowired
    private StoreIdUtils storeIdUtils;

    @Autowired
    private InvInventoryWorkOrderService workOrderService;

    @GetMapping("/{id}")
    @ApiOperation("根据ID查询详情")
    public InvInventoryCheck getById(@PathVariable Long id) {
        return checkService.getById(id);
    }

    @PostMapping("/page")
    @ApiOperation("分页查询详情列表")
    public Page<InvInventoryCheck> page(@RequestBody InvInventoryCheck check) {
        LambdaQueryWrapper<InvInventoryCheck> wrapper = buildQueryWrapper(check);
        return checkService.page(new Page<>(check.getPageNum(), check.getPageSize()),wrapper);
    }

    @DeleteMapping("/{id}")
    @ApiOperation("删除盘点详情")
    public BaseResponse<Boolean> delete(@PathVariable Long id) {
        return BaseResponse.success(checkService.removeById(id));
    }

    /**
     * 导出
     */
    @PostMapping("/export")
    public void export(@RequestBody InvInventoryCheck check, HttpServletResponse response) {
        ExcelUtil<InvInventoryCheck> util = new ExcelUtil<InvInventoryCheck>(InvInventoryCheck.class);
        LambdaQueryWrapper<InvInventoryCheck> wrapper = buildQueryWrapper(check);
        List<InvInventoryCheck> list = checkService.list(wrapper);
        if (!AbdulEmptyUtils.isEmpty(list)){
            Set<Long> workOrderIds = list.stream().map(InvInventoryCheck::getWorkOrderId).collect(Collectors.toSet());
            List<InvInventoryWorkOrder> workOrdersList = workOrderService.list(new LambdaQueryWrapper<InvInventoryWorkOrder>().in(InvInventoryWorkOrder::getId, workOrderIds));
            if (!AbdulEmptyUtils.isEmpty(workOrdersList)){
                Map<Long, String> map = workOrdersList.stream().collect(Collectors.toMap(InvInventoryWorkOrder::getId, InvInventoryWorkOrder::getInventoryNumber));
                for (InvInventoryCheck record : list) {
                    record.setInventoryNumber(map.get(record.getWorkOrderId()));
                }
            }
        }
        util.exportExcel(response,list, "盘点详情数据");
    }


    /**
     * 查询条件封装
     */
    public LambdaQueryWrapper<InvInventoryCheck> buildQueryWrapper(InvInventoryCheck check) {
        LambdaQueryWrapper<InvInventoryCheck> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(!AbdulEmptyUtils.isEmpty(check.getWorkOrderId()), InvInventoryCheck::getWorkOrderId, check.getWorkOrderId());
        wrapper.eq(!AbdulEmptyUtils.isEmpty(check.getProductName()), InvInventoryCheck::getProductName, check.getProductName());
        wrapper.eq(!AbdulEmptyUtils.isEmpty(check.getBarcode()), InvInventoryCheck::getBarcode, check.getBarcode());
        wrapper.eq(!AbdulEmptyUtils.isEmpty(check.getClassification()), InvInventoryCheck::getClassification, check.getClassification());
        wrapper.eq(!AbdulEmptyUtils.isEmpty(check.getCategory()), InvInventoryCheck::getCategory, check.getCategory());
        wrapper.eq(!AbdulEmptyUtils.isEmpty(check.getUnit()), InvInventoryCheck::getUnit, check.getUnit());
        wrapper.eq(!AbdulEmptyUtils.isEmpty(check.getSpecification()), InvInventoryCheck::getSpecification, check.getSpecification());
        wrapper.eq(!AbdulEmptyUtils.isEmpty(check.getInventoryPrice()), InvInventoryCheck::getInventoryPrice, check.getInventoryPrice());
        wrapper.eq(!AbdulEmptyUtils.isEmpty(check.getPhone()), InvInventoryCheck::getPhone, check.getPhone());
        wrapper.eq(!AbdulEmptyUtils.isEmpty(check.getProductManagementId()), InvInventoryCheck::getProductManagementId, check.getProductManagementId());
        wrapper.eq(!AbdulEmptyUtils.isEmpty(check.getInitialStock()), InvInventoryCheck::getInitialStock, check.getInitialStock());
        wrapper.eq(!AbdulEmptyUtils.isEmpty(check.getCheckedQuantity()), InvInventoryCheck::getCheckedQuantity, check.getCheckedQuantity());
        wrapper.eq(!AbdulEmptyUtils.isEmpty(check.getPeriodChange()), InvInventoryCheck::getPeriodChange, check.getPeriodChange());
        wrapper.eq(InvInventoryCheck::getStoreId, storeIdUtils.getStoreId());
        wrapper.orderByDesc(InvInventoryCheck::getUpdateTime);
        return wrapper;
    }
}
